package com.ybfenpan.service.log;


import com.ybfenpan.model.Member;
import com.ybfenpan.model.log.LogMoney;
import com.ybfenpan.model.log.LogMoneyPay;
import com.ybfenpan.service.MemberService;
import com.ybfenpan.service.core.BaseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@Service
public class LogMoneyPayService extends BaseService {
	
	@Resource
    private LogMoneyPayService logMoneyPayService;
	@Resource
    private MemberService memberService;
	@Resource
    private LogMoneyService logMoneyService;
	
	public void savePay(LogMoneyPay logMoneyPay){
		logMoneyPayService.saveOrUpdate(logMoneyPay);
		
		Double num = logMoneyPay.getNum();
		
		/**********************业务操作***************************/
		
		//转账代理商
		Integer fromMemberId = logMoneyPay.getFromMemberId();
		Member fromMember =memberService.getByKey(fromMemberId);
		
		if(fromMember.getMoney()>=num){
			//接收代理商
			Integer toMemberId = logMoneyPay.getToMemberId();
			Member toMember =memberService.getByKey(toMemberId);
			
			
			Map<String, Object> mapMember = new HashMap<String, Object>();
			mapMember.put("id",fromMemberId);
			mapMember.put("diyUpdate","money=money-"+num);
	        //更新余额 
	        memberService.update(mapMember);

	        fromMember= memberService.getByKey(fromMember.getId());
	        Double fromYu = fromMember.getMoney();
	        
	        //日志
	        LogMoney logMoneyFrom = new LogMoney();
	        logMoneyFrom.setMemberId(fromMember.getId());
	        logMoneyFrom.setTitle("转账，接收代理商："+toMember.getLoginName());
	        logMoneyFrom.setState(88);
	        logMoneyFrom.setTotalNum(num);
	        logMoneyFrom.setNum(-num);
	        logMoneyFrom.setYu(fromYu);
	        logMoneyFrom.setDongNum(0d);
	        logMoneyFrom.setFaDate(new Date());
	        logMoneyService.saveOrUpdate(logMoneyFrom);
	        
	        
	        mapMember = new HashMap();
	        mapMember.put("id",toMemberId);
	        mapMember.put("diyUpdate","money=money+"+num);
	        //更新余额 
	        memberService.update(mapMember);
	        
	        toMember= memberService.getByKey(toMember.getId());
	        Double toYu = toMember.getMoney();
	        
	        
	        //日志
	        LogMoney logMoneyTo = new LogMoney();
	        logMoneyTo.setMemberId(toMember.getId());
	        logMoneyTo.setTitle("转账，转账代理商："+fromMember.getLoginName());
	        logMoneyTo.setState(88);
	        logMoneyTo.setTotalNum(num);
	        logMoneyTo.setNum(num);
	        logMoneyTo.setYu(toYu);
	        logMoneyTo.setDongNum(0d);
	        logMoneyTo.setFaDate(new Date());
	        logMoneyService.saveOrUpdate(logMoneyTo);
	        
		}
		
		
         
		
	}
}
